@{block #content}
<script type="text/javascript">
    // OpenTok
    var OpenTokSession;
    var OpenTokPublisher;
    var OpenTokSubscribers = {};
    var OpenTokPublisher;

    $(document).ready(function(){
	if (TB.checkSystemRequirements() != TB.HAS_REQUIREMENTS) {
	    alert('Minimum System Requirements not met!');
	}
	TB.addEventListener('exception', openTokExceptionHandler);
	OpenTokSession = TB.initSession({$openTokSessionID});
	OpenTokSession.addEventListener("sessionConnected", openTokSessionConnectedHandler);
	OpenTokSession.addEventListener("streamCreated", openTokStreamCreatedHandler);

	OpenTokSession.addEventListener('sessionDisconnected', openTokSessionDisconnectedHandler);
	OpenTokSession.addEventListener('connectionCreated', openTokConnectionCreatedHandler);
	OpenTokSession.addEventListener('connectionDestroyed', openTokConnectionDestroyedHandler);

	OpenTokSession.addEventListener('streamDestroyed', openTokStreamDestroyedHandler);
    });

    /**
     * starts video call using OpenTok
     */
    function startVideoCall(){
	OpenTokSession.connect({$openTokAPIKey}, {$openTokToken});
    }

    function stopVideoCall(){
	if (OpenTokPublisher) {
	    OpenTokSession.unpublish(OpenTokPublisher);
	}
	OpenTokPublisher = null;
    }

    function openTokExceptionHandler(event){
	 window.alert("Exception msg:" + event.message +
            "title: " + event.title + " code: " + event.code);
    }
    function openTokSessionDisconnectedHandler(event){}
    function openTokConnectionCreatedHandler(event){}
    function openTokConnectionDestroyedHandler(event){}
    function openTokStreamDestroyedHandler(event){}

    function openTokSessionConnectedHandler (event) {
	//openTokSubscribeToStreams(event.streams);
	OpenTokPublisher = OpenTokSession.publish("celebriotalkerapp_publisher");
	//OpenTokPublisher.addEventListener('settingsButtonClick', showSettings);
    }

    function openTokAddStream(stream) {
	if (stream.connection.connectionId == OpenTokSession.connection.connectionId) {
	    return;
	}
	$('<div class="" id="'+stream.streamId+'"></div>').appendTo("#celebriotalkerapp_streams");
	OpenTokSubscribers[stream.streamId] = OpenTokSession.subscribe(stream, stream.streamId);
    }

    function openTokStreamCreatedHandler(event) {
	//openTokSubscribeToStreams(event.streams);
	for (var i = 0; i < event.streams.length; i++) {
	    openTokAddStream(event.streams[i]);
	}
    }

    function callMessageForm() {
	//if(!nette.validateForm(document.getElementById("frmmessageForm-submited"))) return false;
	/*$.ajax({
	    url: $("#frm-messageForm").attr("action"),
	    dataType: 'json',
	    data: getArrayFromForm("frm-messageForm"),
	    type: "post",
	    success: function (payload) {
		$("#celebriotalkerapp_messageArea").html(payload.snippets["snippet--celebriotalkerMessageAreaSnippet"]);
	    }
	});
	$("#celebriomailapp_settings_snippet").html("");*/
	data = getArrayFromForm("frm-messageForm");
	$("#frmmessageForm-message").val("");
	$("#celebriotalkerapp_messageArea").append("Martin: "+data["message"]+"<br />");
	/*setTimeout(function(){
	    $("#celebriotalkerapp_messageArea").append("Petr: Hi, how are you?<br />");
	},2000);*/
	return false;
    }

    $(document).ready(function(){
       $("#frmmessageForm-submited").click(function(event){
	   event.preventDefault();
	   callMessageForm();
       });
       $("#frmmessageForm-message").keydown(function(e){
	   if (e.keyCode == 13) {
		if (e.ctrlKey) {
		    var val = this.value;
		    if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
			var start = this.selectionStart;
			this.value = val.slice(0, start) + "\n" + val.slice(this.selectionEnd);
			this.selectionStart = this.selectionEnd = start + 1;
		    } else if (document.selection && document.selection.createRange) {
			this.focus();
			var range = document.selection.createRange();
			range.text = "\r\n";
			range.collapse(false);
			range.select();
		    }
		} else {
		    callMessageForm();
		}
		return false;
	    }
       });
    });
</script>
<button onclick="startVideoCall();return false;" title="{!_'Start Video Call'}">{!_'Start Video Call'}</button>
<button onclick="stopVideoCall();return false;" title="{!_'Stop Video Call'}">{!_'Stop Video Call'}</button><br />
<div id="celebriotalkerapp_publisher"></div>
<div id="celebriotalkerapp_streams"></div>
{snippet celebriotalkerMessageAreaSnippet}
<div id="celebriotalkerapp_messageArea">
</div>
{/snippet}
<div id="celebriotalkerapp_messageform">{$messageForm}</div>
{/block}